import $store from '../../../store/index.js';
export default {
	setFileList(data, isPush = true) {
		let list = [];
		if (isPush) {
			list = [...this.getFileList(true)];
			list.push(...data);
		} else {
			list = [...data];
		}
		$store.commit('file_manage/setFileList', list);
	},
	getFileList(isALL, condition) {
		return $store.getters['file_manage/getFileData'](isALL, condition)
	},
	getDirectory() {
		return $store.state.file_manage.directoryList;
	},
	/**
	 * 设置当前目录结构
	 * @param {Array} data
	 * @param {Boolean} isPush 是否使用push
	 */
	setDirectory(data, isPush = true) {
		let list = [];
		if (isPush) {
			list = [...this.getDirectory()];
			list.push(...data);
		} else {
			list = [...data];
		}
		$store.commit('file_manage/setDirectory', list);
	},
	// 获取文件路劲
	getFilePath(fatherId, cureentFileName) {
		let path = [];
		let list = [...this.getFileList(true)];
		filePath(fatherId, list);

		function filePath(fatherId, list) {
			list.forEach(item => {
				if (item._id == fatherId) {
					path.unshift(item.file.name);
					return filePath(item.father_id, list);
				}
			});
		}
		path.unshift('//:');
		path.push(cureentFileName);
		return path.join('/');
	},
	// 获取文件的id以及其子级文件的id
	getFileIds(cureeentId) {
		let list = [...this.getFileList(true)];

		let alls = [];
		/**
		 * 
		 * @param {Arrow} directory 上级目录
		 * @param {*} count 
		 */
		function gets(directory, count = 2) {
			let temp = [];
			directory.forEach(ite => {
				list.forEach(item => {
					if (item.father_id == ite._id) {
						temp.push(item)
					}
				});
			})
			alls.push(...temp);
			if (temp.length > 0) {
				if (count != 1) {
					temp.splice(0, 1);
				}
				gets(temp);
			}
		}
		gets([cureeentId], 1);
		return alls;
	}
}
